﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Extended ImageButton. Represents button for deletion of item.
/// </summary>
public partial class DeleteButton : System.Web.UI.UserControl
{
    /// <summary>
    /// Gets or sets the tool tip.
    /// </summary>
    /// <value>The tool tip.</value>
    public string ToolTip { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="DeleteButton"/> is enabled.
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    public bool Enabled { get; set; }

    /// <summary>
    /// Gets or sets the delete confirmation text.
    /// </summary>
    /// <value>The delete confirmation text.</value>
    public string DeleteConfirmationText { get; set; }

	/// <summary>
	/// Gets or sets the name of the command.
	/// </summary>
	/// <value>The name of the command.</value>
	public string CommandName { get; set; }

	/// <summary>
	/// Gets or sets the command argument.
	/// </summary>
	/// <value>The command argument.</value>
	public string CommandArgument { get; set; }

    /// <summary>
    /// Occurs when [click].
    /// </summary>
    public event ImageClickEventHandler Click;

	/// <summary>
	/// Occurs when [command].
	/// </summary>
	public event CommandEventHandler Command;

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(this.ToolTip))
        {
            this.Button.ToolTip = this.ToolTip;
        }
        this.Button.Click += new ImageClickEventHandler(OnClick);
		this.Button.Command += new CommandEventHandler(OnCommand);
		this.Button.CommandName = this.CommandName;
		this.Button.CommandArgument = this.CommandArgument;
        this.Button.Enabled = this.Enabled;
        if (String.IsNullOrEmpty(this.DeleteConfirmationText))
        {
            this.Button.OnClientClick = (string)GetLocalResourceObject("DeleteConfirmation");
        }
    }

    /// <summary>
    /// Called when [click].
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
    public void OnClick(object sender, ImageClickEventArgs e)
    {
        if (this.Click != null)
        {
            this.Click(this, e);
        }
    }

	/// <summary>
	/// Called when [command].
	/// </summary>
	/// <param name="sender">The sender.</param>
	/// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
	protected void OnCommand(object sender, CommandEventArgs e)
	{
		if (this.Command != null)
		{
			this.Command(this, new CommandEventArgs(this.CommandName, this.CommandArgument));
		}
	}
}
